﻿// -----------------------------------------------------------------------
// <copyright file="RunningApp.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TouchExplorer.Entities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
using System.Diagnostics;
    using Microsoft.WindowsAPICodePack.Taskbar;
    using System.Windows.Media.Imaging;
    using System.Runtime.InteropServices;
    using System.Windows;
    using TouchExplorer.Controls;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class RunningApp : IDisposable
    {
        public Process RunningProcess { get; set; }

        public Thumbnail Content { get; set; }

        public string Name { get; set; }

        public IntPtr WndHnd { get; set; }


        public RunningApp(Process proc)
        {
            RunningProcess = proc;
            Name = proc.MainWindowTitle;            
        }

        public RunningApp()
        {
            
        }

        ~RunningApp()
        {
            this.Dispose(false);
            GC.SuppressFinalize(this);
        }

        public void LoadImage()
        {
            Content = null;
            
            //TabbedThumbnail thumb = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(RunningProcess.MainWindowHandle);
            //if (thumb != null)
            //{
            //    Bitmap = new BitmapImage();
            //    thumb.SetImage(Bitmap);
            //}
        }

        public void Activate()
        {            
            SetForegroundWindow(WndHnd);
        }

        public void Dispose()
        {
            this.Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (RunningProcess != null)
                RunningProcess.Dispose();

            Content = null;
        }

        public override string ToString()
        {
            return Name;
        }

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

    }
}
